#!/usr/bin/env python
import os
import pycurl
from xml.dom import minidom

import pygtk
pygtk.require('2.0')
import gtk
import gtk.glade

class XmlResponse:
   def __init__(self):
       self.contents = ''

   def body_callback(self, buf):
       self.contents = self.contents + buf

class ResponseDialog():
	def __init__(self,settings):
		
		#Set the Glade file
		self.gladefile = os.path.join(os.getcwd(), 'imgur.glade')
		self.wTree = gtk.glade.XML(self.gladefile) 
		
		#Get the Main Window, and connect the "destroy" event
		self.window = self.wTree.get_widget("dialog1")

		self.wTree.get_widget("link_original_image").set_label(settings["original_image"])
		self.wTree.get_widget("link_large_thumbnail").set_label(settings["large_thumbnail"])
		self.wTree.get_widget("link_small_thumbnail").set_label(settings["small_thumbnail"])
		self.wTree.get_widget("link_imgur_page").set_label(settings["imgur_page"])
		self.wTree.get_widget("link_delete_page").set_label(settings["delete_page"])

		self.wTree.get_widget("link_original_image").set_uri(settings["original_image"])
		self.wTree.get_widget("link_large_thumbnail").set_uri(settings["large_thumbnail"])
		self.wTree.get_widget("link_small_thumbnail").set_uri(settings["small_thumbnail"])
		self.wTree.get_widget("link_imgur_page").set_uri(settings["imgur_page"])
		self.wTree.get_widget("link_delete_page").set_uri(settings["delete_page"])

		#Create our dictionay and connect it
		dic = { "on_MainWindow_destroy" : self.hide_window,
				"on_btnClose_clicked" : self.hide_window
			}
		self.wTree.signal_autoconnect(dic)
		
		self.window.connect("destroy", gtk.main_quit)

	def show(self):
	    self.window.show()
	    gtk.main()

	def hide_window(self,widget):
		self.window.hide()
		gtk.main_quit()
		self.window.destroy()


def usage():
	print 'imgur_upload [path-to-file]'

if len(os.sys.argv) == 2:
	filename = os.sys.argv[1]
	c = pycurl.Curl()
	values = [
		  ("key", "a3793a1cce95f32435bb002b92e0fa5e"),
		  ("image", (c.FORM_FILE, filename))]
	# OR:     ("image", "http://example.com/example.jpg"))]

	c.setopt(c.URL, "http://imgur.com/api/upload.xml")
	c.setopt(c.HTTPPOST, values)
	t = XmlResponse()	
	c.setopt(c.WRITEFUNCTION, t.body_callback)

	c.perform()
	c.close()

	dom = minidom.parseString(t.contents)

	settings = {
		'original_image' : dom.getElementsByTagName("original_image")[0].childNodes[0].nodeValue,
		'large_thumbnail' : dom.getElementsByTagName("large_thumbnail")[0].childNodes[0].nodeValue,
		'small_thumbnail' : dom.getElementsByTagName("small_thumbnail")[0].childNodes[0].nodeValue,
		'imgur_page' : dom.getElementsByTagName("imgur_page")[0].childNodes[0].nodeValue,
		'delete_page' : dom.getElementsByTagName("delete_page")[0].childNodes[0].nodeValue,
	}
	
	#automatically place link to clipboard
	clipboard = gtk.clipboard_get()
	clipboard.set_text(settings['original_image'])
	clipboard.store()
	
	r = ResponseDialog(settings)
	r.show()

else:
	usage()



#for arg in sys.argv:
	

"""



##! /bin/sh 
#
#TEXT=$(curl -F "image"=@"$1" -F "key"="a3793a1cce95f32435bb002b92e0fa5e" http://imgur.com/api/upload.xml | sed -e "s/.*<imgur_page>//" | sed -e "s/<.*//")
#zenity --info --title="Imgur Upload" --text="$TEXT"
"""
